package com.sjk.flink.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Iterator;

import static com.fasterxml.jackson.databind.PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES;

/**
 * 大小写
 */
public class TranstoUpperOrLowerCaseUtil {
    /**
     * json的key转大/小写
     *
     * @param jsonStr
     * @return
     */
    public static String transtoLowerCaseObject(String jsonStr) throws JsonProcessingException {
        ObjectMapper mapper = ObjectMapperUtil.getObjectMapper();
        JsonNode node = mapper.readTree(jsonStr);
        JsonNode jsonNode = lowercaseNodeNames(mapper,node);
        return mapper.writeValueAsString(jsonNode);
    }

    public static void main(String[] args) throws JsonProcessingException {
        String json = "{'Wfff':'a','Zffff':'b','Y44':'c'}";
        String jsonArray = "[{'name':'a', 'level':'high', 'student':{'name':'11','xxx':'22','wwwa':'33'}}]";
        ObjectMapper mapper = ObjectMapperUtil.getObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setPropertyNamingStrategy(new MyNameStrategy());
        JsonNode node = mapper.readTree(json);
        /**
        ObjectNode objectNode = (ObjectNode) node;
        objectNode.fields().forEachRemaining((entry) ->objectNode.set(entry.getKey().toLowerCase(), entry.getValue())
        );
         **/
        JsonNode jsonNode = lowercaseNodeNames(mapper,node);
        String jsonString = mapper.writeValueAsString(jsonNode);
        System.out.println(jsonString);
        //json的key转大写
        //System.out.println(transtoUpperOrLowerCaseObject(json, 1));
        //json数组的key转大写
        //System.out.println(transToUpperOrLowerCaseArray(jsonArray, 1).toString());
    }
    private static JsonNode lowercaseNodeNames(ObjectMapper mapper,JsonNode node) {
        //ObjectMapper mapper = new ObjectMapper();
        ObjectNode loweredNode = mapper.createObjectNode();
        Iterator<String> fieldNames = node.fieldNames();
        while (fieldNames.hasNext()) {
            String currentName = fieldNames.next();
            JsonNode currentValue = null;
            if (node.get(currentName).isObject()) {
                // checking for nestedNode in currentNode
                JsonNode nestedNode = node.get(currentName);
                currentValue = lowercaseNodeNames(mapper,nestedNode);
            } else if (node.get(currentName).isArray()) {
                ArrayNode arrayNode = mapper.createArrayNode();
                for (int i = 0; i < node.get(currentName).size(); i++) {
                    arrayNode.add(lowercaseNodeNames(mapper,node.get(currentName).get(i)));
                }
                currentValue = arrayNode;
            } else {
                currentValue = node.get(currentName);
            }
            // lowercase only the first character of the name
            loweredNode.put(Character.toString(currentName.charAt(0)).toLowerCase() + currentName.substring(1), currentValue);
        }
        return loweredNode;
    }

}
